﻿using System;
using PersonalAccoutingProgram.Data;
using System.Linq;
using System.Windows.Forms;

public class DataValidator
{ 
    private static bool isUsernameLenghtValid(string username)
    {
        if (username.Length < 4 || username == String.Empty)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    private static bool isPasswordLenghtValid(string password)
    {
        if (password.Length < 6 || password == String.Empty)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public static bool isUserInputValid(
        string username, string password)
    {
        
        ValidateUsername(username);
        ValidatePassword(password);
        isThereSuchUsername(username);

        return true;
    }

    private static void ValidatePassword(
        string password)
    {
        if (!isPasswordLenghtValid(password))
        {
            throw new ArgumentException("Password is too short or empty!");           
        }
        
    }

    private static void ValidateUsername(
        string username)
    {
        if (username == String.Empty)
        {
            throw new ArgumentException("Not entered username!");
        }
        if (!isUsernameLenghtValid(username))
        {
            throw new ArgumentException("Username is too short!");         
        }     
    }
 
    public static bool isUserInformationCorrect(
        string username, string password)
    {
        var user = UserDataClassesDataContext.GetUserByPass(username, password);
  
        if (user.Count(currentUser => currentUser.Username.Length != null) == 0)
        {
            throw new ArgumentException("Incorrect username or password!"); 
        }
        
        return true;
    }

    public static void isThereSuchUsername(string username)
    {
        var queryResult = UserDataClassesDataContext.GetUser(username);

        if (queryResult.Count(user => user.Username.Length != null) > 0)
        {
            throw new ArgumentException("This username already exists!");                  
        }        
    }
   
    public static bool isAmountValid(string amountStr)
    { 
        if (amountStr == String.Empty)
        {
            throw new ArgumentException("The amount is empty!");
        }
        try
        {
            decimal number = decimal.Parse(amountStr);
        }
        catch (FormatException)
        {
            throw new FormatException("Invalid amount!");
        }

        return true;
    }

    public static void ValidateAmount(KeyPressEventArgs e)
    {
        bool isValidType = Char.IsDigit(e.KeyChar) || 
                           Char.IsControl(e.KeyChar) || 
                           e.KeyChar == '.';
        if (!isValidType)
        {
            e.Handled = true;
        }
    }

    public static void ValidateUsername(KeyPressEventArgs e)
    {
        bool isValidType = Char.IsLetterOrDigit(e.KeyChar) ||
                           Char.IsControl(e.KeyChar);
        if (!isValidType)
        {
            e.Handled = true;
        }
    }

    public static void ValidatePassword(KeyPressEventArgs e)
    {
        bool isValidType = Char.IsLetterOrDigit(e.KeyChar) ||
                           Char.IsControl(e.KeyChar);
        if (!isValidType)
        {
            e.Handled = true;
        }
    }
}